﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;

public partial class TaoBaoApi_TB_Color : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            BindColor();
        }
    }
    void BindColor()
    {
        ddlColor.Items.Clear();
        ddlTBColor.Items.Clear();
        string sql = "select mid,tb_mc from tb_guige1 order by mid";
        DataSet ds = DBUtility.SqlHelper.Query(DBUtility.SqlHelper.LocalSqlServer, sql);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                ddlTBColor.Items.Add(new ListItem(ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][0].ToString()));
        sql = "select ggdm,ggmc from guige1  ";
        ds = DBUtility.SqlHelper.Query(DBUtility.SqlHelper.LocalSqlServer, sql);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                ddlColor.Items.Add(new ListItem(ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][0].ToString()));
    }
    void Bind()
    {
        string sql = "select mid,field1,tb_mc from tb_guige1 where field1 is not null order by mid desc";
        DataSet ds = DBUtility.SqlHelper.Query(DBUtility.SqlHelper.LocalSqlServer, sql);
        gvList.DataSource = ds.Tables[0];
        gvList.DataBind();
    }
    protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvList.PageIndex = e.NewPageIndex;
        Bind();
    }
    protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
    {
        new TBCommon().gv_RowCreated(sender, e, gvList, Page);
    }
    protected void chkAll_OnCheckedChanged(object sender, EventArgs e)
    {
        new TBCommon().chk_OnCheckedChanged(sender, e, gvList, "chkOne");
    }
    protected string GetBDColorName(object o)
    {
        if (o is DBNull || o == null || o.ToString().Length == 0)
            return null;
        return new BLL.GUIGE1().GetMC(o.ToString());
    }
    //编辑选定关联
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        int c = 0;
       int mid=0;
        foreach (GridViewRow row in gvList.Rows)
        {
            CheckBox chk = (CheckBox)row.FindControl("chkOne");
            if (chk != null && chk.Checked)
            {
                c++;
                mid=Convert.ToInt32(row.Cells[1].Text);
            }
        }
        if (c == 0)
        {
            new TBCommon().ShowMessage(Page, "请选择一条记录。");
            return;
        }
        if (c > 1)
        {
            new TBCommon().ShowMessage(Page, "一次只能编辑一条记录。");
            return;
        }
        if (mid > 0)
        {
            ddlTBColor.SelectedValue = mid.ToString();
            ddlColor.SelectedValue = new BLL.TaoBao.TB_GUIGE1().GetModel(mid).field1;
        }
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {     
        List<int> midList = new List<int>();
        foreach (GridViewRow row in gvList.Rows)
        {
            CheckBox chk = (CheckBox)row.FindControl("chkOne");
            if (chk != null && chk.Checked)            
                midList.Add( Convert.ToInt32(row.Cells[1].Text));        
        }
        if (midList.Count == 0)
        {
            new TBCommon().ShowMessage(Page, "请选择一条记录。");
            return;
        }
        foreach (int m in midList)
        {
            Model.TaoBao.TB_GUIGE1 g = new BLL.TaoBao.TB_GUIGE1().GetModel(m);
            if (g != null)
            {
                g.field1 = null;
                new BLL.TaoBao.TB_GUIGE1().Update(g);
            }
        }        
        Response.Redirect("tb_color.aspx");
    }
    //关联
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (ddlTBColor.SelectedIndex < 0 || ddlColor.SelectedIndex < 0)
        {
            new TBCommon().ShowMessage(Page, "请选择淘宝颜色和本地颜色。");
            return;
        }
        int mid = Convert.ToInt32(ddlTBColor.SelectedValue);
        Model.TaoBao.TB_GUIGE1 g = new BLL.TaoBao.TB_GUIGE1().GetModel(mid);
        if (g != null)
        {
            g.field1 = ddlColor.SelectedValue;
            new BLL.TaoBao.TB_GUIGE1().Update(g);           
        }        
        Response.Redirect("tb_color.aspx");
    }
}
